<?php

class GroupAPIController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
	}
	
	/**
	 * Display a listing of groups in JSON format.
	 *
	 * @return JSON Response
	 */
	public function index()
	{
		return Response::json(Group::all());
	}


	/**
	 * Show the form for creating a new group.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created group in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$newGroup = Input::json();
	
		$group = new Group();
		$group->name = $newGroup->get('name');
		$group->image = $newGroup->get('image');
		$group->admin_userId = $newGroup->get('admin_userId');
		$group->save();
	
		$userIds = array($group->admin_userId);
		$group->users()->sync($userIds);
		
		return Response::json($group);
	}


	/**
	 * Display the specified group.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$group = Group::find($id);
		if(is_null($group)){
			return Response::json('Group not found', 404);
		} else {
			$group->users;
			$group->admin;
			return Response::json($group);
		}
	}


	/**
	 * Show the form for editing the specified group.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified group in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$updateGroup = Input::json();
		
		$group = Group::find($id);
		if(is_null($group)){
			return Response::json('Group not found', 404);
		}
		$group->name = $updateGroup->get('name');
		$group->image = $updateGroup->get('image');
		$group->admin_userId = $updateGroup->get('admin_userId');
		$group->save();
	
		return Response::json($group);
	}


	/**
	 * Remove the specified group from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$group = Group::find($id);
	
		if(is_null($group))
		{
			return Response::json('Group not found', 404);
		}
		$deletedGroup = $group;
		$group->delete();
		return Response::json($deletedGroup);
	}
	
	/**
	 * Display a listing of users in the specified group in JSON format.
	 *
	 * @param groupId
	 * @return JSON Response
	 */
	public function getUsers($groupId)
	{
		$group = Group::find($groupId);
		if(is_null($group)){
			return Response::json('Group not found', 404);
		} else {
			$group->admin;
			$group->users;
			return Response::json($group);
		}
	}
	
	/**
	 * Update specified users into specified group by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateUsers()
	{
		$data = Input::json();
	
		// store relation to User
		$groupId = $data->get('groupId');
		$group = Group::find($groupId);
		$userFBIds = $data->get('userIds');
		if(is_array($userFBIds))
		{
			$userIds = array();
			foreach($userFBIds as $userFBId){
				$user = User::where('fbId', '=', $userFBId)->first();
				$userIds[] = $user->id;
			}
			$group->users()->sync($userIds);
		}
	
		$group->admin;
		$group->users;
		return Response::json($group);
	}

}
